package com.jungle.client;

import com.jungle.bfi.domain.vo.VoucherTypeVo;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.scene.control.*;
import javafx.scene.input.InputMethodRequests;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;

import java.util.function.Consumer;

import static com.jungle.client.FxApplication.bundle;

public class VoucherTypeEditController {
    private static final String TITLE = bundle.getString("voucherType.title");

    public static void showAddDialog(VoucherTypeVo dataSetVo, Consumer<VoucherTypeVo> consumer) {

        String title = bundle.getString("dialog.title.prefix.add") + TITLE;

        new VoucherTypeEditController().showDialog(title, dataSetVo, consumer);
    }

    public static void showEditDialog(VoucherTypeVo dataSetVo, Consumer<VoucherTypeVo> consumer) {

        String title = bundle.getString("dialog.title.prefix.edit") + TITLE;

        new VoucherTypeEditController().showDialog(title, dataSetVo, consumer);
    }

    private void showDialog(String title, VoucherTypeVo dataSetVo, Consumer<VoucherTypeVo> consumer) {
        Dialog<VoucherTypeVo> dialog = new Dialog<>();
        dialog.setTitle(title);

        DialogPane dialogPane = dialog.getDialogPane();
        dialogPane.setContent(buildGridPane());

        loadData(dataSetVo);

        dialogPane.getButtonTypes().addAll(ButtonType.OK, ButtonType.CANCEL);


        dialog.setResultConverter(buttonType -> {
            if (buttonType == ButtonType.OK) {
                return storeData();
            }
            return null;
        });

        dialog.showAndWait().ifPresent(consumer);
    }

    private GridPane buildGridPane() {
        GridPane gridPane = new GridPane();
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        gridPane.setPadding(new Insets(10, 10, 10, 10));

        // 设置列约束
        ColumnConstraints column1 = new ColumnConstraints(100, 150, Double.MAX_VALUE);
        column1.setHgrow(Priority.NEVER);

        ColumnConstraints column2 = new ColumnConstraints(100, 150, Double.MAX_VALUE);
        column2.setHgrow(Priority.ALWAYS);

        gridPane.getColumnConstraints().addAll(column1, column2);

        gridPane.add(new Label(bundle.getString("voucherType.info.name")), 0, 0);
        gridPane.add(new Label(bundle.getString("voucherType.info.value")), 0, 1);
        nameField.setPromptText(bundle.getString("voucherType.info.name"));
        nameField.setInputMethodRequests(imr);
        valueField.setInputMethodRequests(imr);

        gridPane.add(nameField, 1, 0);
        gridPane.add(valueField, 1, 1);

        return gridPane;
    }
    private final InputMethodRequests imr = new InputMethodRequests() {
        @Override
        public Point2D getTextLocation(int i) {
            return null;
        }
        @Override
        public int getLocationOffset(int i, int i1) {
            return 0;
        }
        @Override
        public void cancelLatestCommittedText() {
        }
        @Override
        public String getSelectedText() {
            return "";
        }
    };
    private final com.jungle.client.control.TextField nameField = new com.jungle.client.control.TextField();
    private final TextField valueField = new TextField();

    private String id;

    private void loadData(VoucherTypeVo dataVo) {
        id = dataVo.getId();
        nameField.setText(dataVo.getName());
        valueField.setText(dataVo.getValue());
    }

    private VoucherTypeVo storeData() {
        VoucherTypeVo dataVo = new VoucherTypeVo();
        dataVo.setId(id);
        dataVo.setName(nameField.getText());
        dataVo.setValue(valueField.getText());
        return dataVo;
    }
}
